package de.hbrs.ooka.crs.servlet;

import de.hbrs.ooka.beans.ConferenceBeanStatelessLocal;
import de.hbrs.ooka.beans.UserSessionStatefulLocal;
import de.hbrs.ooka.crs.servlet.constants.WebPageNames;
import de.hbrs.ooka.entities.UserEntity;
import de.hbrs.ooka.enums.Role;
import de.hbrs.ooka.exception.BadReputationException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ConferenceAddingServlet extends AbstractViewServlet {

    @EJB
    private ConferenceBeanStatelessLocal conferenceBean;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        UserSessionStatefulLocal crsBean = this.getCrsBean(request);
        String redirection = "";
        RequestDispatcher rd = null;

        if (crsBean == null || !this.isUserAuthenticated(crsBean.getUser(), Role.ORGANIZER)) {
            request.setAttribute("message", "Not logged in");
            request.setAttribute("redirect", WebPageNames.LoginViewPage);
            rd = request.getRequestDispatcher("/" + WebPageNames.ErrorPage + ".jsp");
            if (rd != null) {
                rd.forward(request, response);
            }
        } else {
            String action = request.getParameter("action");
            if (action == null) {
                rd = request.getRequestDispatcher("/" + WebPageNames.ConferenceAddingPage + ".jsp");
                if (rd != null) {
                    rd.forward(request, response);
                }
            } else if (action.equalsIgnoreCase("addconference")) {
                String adr = request.getParameter("adr");
                String date = request.getParameter("date");
                int count = Integer.parseInt(cutStringToNumberOnly(request.getParameter("max")));
                String confName = request.getParameter("conf");

                SimpleDateFormat f = new SimpleDateFormat("dd.MM.yyyy");
                try {
                    Date d = f.parse(date);

                    UserEntity user = crsBean.getUser();
                    if (this.conferenceBean.addConference(user, confName, adr, d, count) != null) {
                        request.setAttribute("redirect", WebPageNames.OrganizerViewPage);
                        rd = request.getRequestDispatcher("/" + WebPageNames.ActionSuccessfulPage);
                        if (rd != null) {
                            rd.forward(request, response);
                        }
                        //response.sendRedirect(WebPageNames.ActionSuccessfulPage);
                    } else {
                        response.sendRedirect(WebPageNames.ErrorPage + ".jsp");
                        // FIXME: zurück zum organizerview schicken
                    }
                } catch (BadReputationException ex) {
                    // Fehlermeldung anzeigen
                    System.err.println("Exception handling vollziehen !!!");
                    request.setAttribute("redirect", WebPageNames.OrganizerViewPage);
                    request.setAttribute("message", "NEIN!!!");
                    //response.sendRedirect(WebPageNames.ErrorPage + ".jsp");
                    
                    rd = request.getRequestDispatcher("/" + WebPageNames.ErrorPage + ".jsp");
                    if (rd != null) {
                        rd.forward(request, response);
                    }
                    
                } catch (ParseException ex) {
                    Logger.getLogger(ConferenceAddingServlet.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    private String cutStringToNumberOnly(String src) {
        String result = "0";
        for (int i = 0; i < src.length(); ++i) {
            if (src.charAt(i) >= '0' && src.charAt(i) <= '9') {
                result += src.charAt(i);
            }
        }
        return result;
    }
}
